<?php

namespace app\lib\exception;
use bingher\ding\DingBot;
use think\Exception;
use think\Config;
use think\exception\Handle;
/**
 * 自定义异常类
 */
class ExceptionHandler extends Handle
{
    /**
     * http状态码
     * @var unknown
     */
    public $httpCode = 500;

    public function render(\Exception $e)
    {

        $debug_status = config("app_debug");

        if ($debug_status) {
            return parent::render($e);
        } else {
            $ding  =new DingBot();
            $req = request();
//            dump($req);exit;
            $url = $req->url(true);
            $param = $req->param(true);
            $server = $req->server()['HTTP_USER_AGENT'];
            $txt = '报错信息:'.$e->getMessage()."\n".'报错状态码:'.$this->httpCode."\n".'报错地址:'.$url."\n".'报错参数:'.json_encode($param)
                ."\n".'报错设备:'.$server;

            $res = $ding->atAll()->text($txt);
            return $this->show(2, $e->getMessage(), [], $this->httpCode);
        }

    }

    public function show($status, $message ,$data = [] ,$httpCode = 200)
    {
        $data =  [
            'status' => $status,
            'errorcode'=>'100000',
            'msg' => $message,
            'result' =>$data
        ];
        return json($data, $httpCode);
    }
    function get_runtime(){ $ntime=microtime(true); $total=$ntime-$GLOBALS['_beginTime']; return round($total,4); }
}